package cn.bore.tomato.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.bore.common.idempotent.annotation.RepeatSubmit;
import cn.bore.common.log.annotation.Log;
import cn.bore.common.web.core.BaseController;
import cn.bore.common.mybatis.core.page.PageQuery;
import cn.bore.common.core.domain.R;
import cn.bore.common.core.validate.AddGroup;
import cn.bore.common.core.validate.EditGroup;
import cn.bore.common.log.enums.BusinessType;
import cn.bore.common.excel.utils.ExcelUtil;
import cn.bore.tomato.domain.vo.TmsSessionsVo;
import cn.bore.tomato.domain.bo.TmsSessionsBo;
import cn.bore.tomato.service.ITmsSessionsService;
import cn.bore.common.mybatis.core.page.TableDataInfo;

/**
 * 番茄记录
 *
 * @author 范亚鹏
 * @date 2025-08-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/tomato/sessions")
public class TmsSessionsController extends BaseController {

    private final ITmsSessionsService tmsSessionsService;

    /**
     * 查询番茄记录列表
     */
    @SaCheckPermission("tomato:sessions:list")
    @GetMapping("/list")
    public TableDataInfo<TmsSessionsVo> list(TmsSessionsBo bo, PageQuery pageQuery) {
        return tmsSessionsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出番茄记录列表
     */
    @SaCheckPermission("tomato:sessions:export")
    @Log(title = "番茄记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TmsSessionsBo bo, HttpServletResponse response) {
        List<TmsSessionsVo> list = tmsSessionsService.queryList(bo);
        ExcelUtil.exportExcel(list, "番茄记录", TmsSessionsVo.class, response);
    }

    /**
     * 获取番茄记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("tomato:sessions:query")
    @GetMapping("/{id}")
    public R<TmsSessionsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tmsSessionsService.queryById(id));
    }

    /**
     * 新增番茄记录
     */
    @SaCheckPermission("tomato:sessions:add")
    @Log(title = "番茄记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TmsSessionsBo bo) {
        return toAjax(tmsSessionsService.insertByBo(bo));
    }

    /**
     * 修改番茄记录
     */
    @SaCheckPermission("tomato:sessions:edit")
    @Log(title = "番茄记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TmsSessionsBo bo) {
        return toAjax(tmsSessionsService.updateByBo(bo));
    }

    /**
     * 删除番茄记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("tomato:sessions:remove")
    @Log(title = "番茄记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tmsSessionsService.deleteWithValidByIds(List.of(ids), true));
    }
}
